<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\2\23 0023
 * Time: 17:30
 */

namespace app\Lib\Model\Present;


use app\Lib\Model\Img\Img;

class Present implements \app\Lib\InterfaceIn\Present\Present
{
    private $presentId;
    private $cache;
    private $cacheName;
    private $table;
    private $filed = 'id';
    public function __construct($presentId = 0, $cache = true)
    {
        $this->presentId = $presentId;
        $this->cache = $cache;
        $this->cacheName ='lottery_present_present_id_'.$this->presentId;

    }

    /**
     * 中奖几率
     * @return int
     */
    public function getProbabilityNum()
    {
        $this->filed = $this->getTable()->_probability_num;
        return  $this->getFiledValue();
    }

    /**
     * 奖品种类
     * @return string
     */
    public function getPresentType()
    {
        $this->filed = $this->getTable()->_present_type;
        return  $this->getFiledValue();
    }

    /**
     * 奖品名称
     * @return string
     */
    public function getPresentName()
    {
        $this->filed = $this->getTable()->_present_name;
        return  $this->getFiledValue();
    }

    /**
     * 奖品图片
     * @return string
     */
    public function getPresentImg()
    {
        $this->filed = $this->getTable()->_present_img_id;
        $img = $this->getFiledValue();
        return (new Img($img))->getFilePath();
    }

    /**
     * 奖品数量
     * @return string
     */
    public function getPresentNum()
    {
        $this->filed = $this->getTable()->_present_num;
        return  $this->getFiledValue();
    }

    /**
     * 奖品id
     * @return int
     */
    public function getPresentId()
    {
        return $this->presentId;
    }


    private function getTable(){
        if(empty($this->table)){
            $this->table = new \app\Lib\Model\DbModel\Present();
        }
        return $this->table;
    }

    private function getFiledValue(){
        if($this->cache){
            $result = $this->getCacheValue();
        }else{
            $result = $this->getDbFiledValue();
        }
        return $result;
    }

    private function getDbFiledValue(){
        $table  = $this->getTable();
        $result = $table->where([$table->_id=>$this->presentId])->value($this->filed);
        return empty($result) && $result!= '0' ?'null':$result;
    }

    private function getCacheValue(){
        $cache  = $this->getAllCacheName();
        $result = cache($cache);
        if(empty($result)){
            $result = $this->getDbFiledValue();
            cache($cache,$result,604800);
        }
        return $result;
    }

    public function delCache(){
        //清除userBase缓存
        $array = $this->getTable()->getTableFields();
        foreach ($array as $v){
            $this->filed = $v;
            $allCacheName = $this->getAllCacheName();
            cache($allCacheName,null);
        }
        return true;
    }

    private function getAllCacheName(){
        return md5($this->cacheName.'_'.$this->filed);
    }
}